package com.epoint.androidphone.mobileoa.ui.main;

import java.util.HashMap;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.epoint.androidmobile.bizlogic.dbservice.ConfigKey;
import com.epoint.androidmobile.bizlogic.syn.task.SynTask;
import com.epoint.androidmobile.core.control.ToastUtil;
import com.epoint.androidmobile.core.phoneutil.PhoneHelp;
import com.epoint.androidphone.R;
import com.epoint.androidphone.mobileoa.service.MainService;
import com.epoint.androidphone.mobileoa.ui.superview.SuperPhonePage;

/**
 * @author liyc
 * @time @2013-5-5 下午11:42:58
 * @annomation 
 */
public class MainView extends SuperPhonePage{
	
	private GridView gv;
	List<GVIcon> list;
	long synDataTaskId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		/**
		 * 开启Service
		 */
		Intent intent = new Intent(MainService.ServiceName);
		startService(intent);
		
		addContentViewAndTitle(R.layout.mainview, getString(R.string.app_name)+PhoneHelp.getVersionName(this));
		getIvLeft().setVisibility(View.GONE);
		gv = (GridView) findViewById(R.id.gv);
		gv.setOnItemClickListener(this);
		list = new MainViewIconInstance().iconDataSource(this);
		GVAdapter adapter = new GVAdapter();
		gv.setAdapter(adapter);
		
		int usernums = dbUtil.getUserNums();
		int ounums = dbUtil.getOuCount();
		if(usernums==0||ounums==0)
		{
			showDialogMiddle("正在同步数据...");
			HashMap<String, Object> param = getPassMap();
			param.put("ParentOUGuid", "");
			synDataTaskId = new SynTask(this, param).startTask();
		}
		
		String NewWaitHandleCountStr = getIntent().getStringExtra("NewWaitHandleCount");
		String NewMailsCountStr = getIntent().getStringExtra("NewMailsCount");
		
		int NewWaitHandleCount = Integer.parseInt(NewWaitHandleCountStr);
		int NewMailsCount = Integer.parseInt(NewMailsCountStr);
		
		String msg = dbUtil.getDisplayName()+"，您好！";
		if(NewWaitHandleCount>0)
		{
			msg += "<br/>您当前有<font color=red>"+NewWaitHandleCount+"</font>条待办事项！";
		}
		if(NewMailsCount>0)
		{ 
			msg += "<br/>您当前有<font color=red>"+NewMailsCount+"</font>封新邮件！";
		}
		if(NewWaitHandleCount>0||NewMailsCount>0)
		{
			ToastUtil.toastLong_Html(this, msg);
		}
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO 自动生成的方法存根
		super.onItemClick(parent, view, position, id);
		startActivity(list.get(position).intent);
	}
	
	
	class GVAdapter extends BaseAdapter
	{

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				LayoutInflater mInflater = LayoutInflater.from(getContext());
				convertView = mInflater.inflate(R.layout.iconlayout, null);

				holder.iv = (ImageView) convertView
						.findViewById(R.id.ivIcoImg);
				holder.tv = (TextView) convertView.findViewById(R.id.tvIcoTitle);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			GVIcon iifo = list.get(position);

			holder.iv.setImageResource(iifo.iconImage);
			holder.tv.setText(iifo.iconTitle);
			holder.tv.setTextColor(getResources().getColor(R.color.black));
			return convertView;
		}
		
	}
	
	public class ViewHolder {
		public ImageView iv;
		public TextView tv;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			new AlertDialog.Builder(this)
			.setTitle("提示")
			.setMessage("您是否要退出软件？")
			.setPositiveButton("确定", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
					MainService.isCheckTreadRun = false;
					dbUtil.setConfigValue(ConfigKey.isLogin, "0");
					Intent intent = new Intent(MainService.ServiceName);
					stopService(intent);
					System.exit(0);
				}
			})
			.setNegativeButton("取消", null)
			.create().show();
			break;
		}
		return false;
	}
	
}
